package com.winxuan.channel.export.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.taobao.api.domain.Item;
import com.taobao.api.request.ItemGetRequest;
import com.taobao.api.request.ItemUpdateRequest;
import com.taobao.api.response.ItemGetResponse;
import com.taobao.api.response.ItemUpdateResponse;
import com.winxuan.channel.export.service.ECService;
import com.winxuan.channel.export.taobao.NameConvert;
import com.winxuan.channel.export.taobao.TaobaoEnv;
import com.winxuan.channel.model.commodity.CommoditySnap;
import com.winxuan.channel.service.commodity.SnapService;
import com.winxuan.channel.service.commodity.impl.CommoditySanpFinder;
import com.winxuan.support.dao.Finder;
import com.winxuan.util.pagination.Pagination;

public class UpdateTaobaoItem extends TaobaoEnv{

	private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring/global-app.xml");

	SnapService snapService = (SnapService) applicationContext.getBean("snapService");
	ECService ecService = (ECService) applicationContext.getBean("ecService");

	NameConvert nameConvert = new NameConvert();

	public void getCommodity() throws ParseException{
		int page = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("distribute",110);
		map.put("beginLastUpdateTime",SimpleDateFormat.getDateTimeInstance().parse("2011-06-25 22:31:00"));
		Finder finder = new CommoditySanpFinder(map);
		Pagination pagination = new Pagination();
		pagination.setPageSize(20);
		pagination.setCurrentPage(page);
		List<CommoditySnap> snaps = null;
		while((snaps = snapService.getCommoditySnapList(finder, pagination))!=null){
			System.out.println(page);
			for(CommoditySnap snap : snaps){
				processCommodity(snap);
			}
			pagination.setCurrentPage(page++);
		}
		System.out.println(snaps.size());
	}

	private void processCommodity(CommoditySnap snap){
		Integer goodsId = snap.getCommoditySnapPK().getCommodity();
		String name = ecService.getEcCommodityName(goodsId);
		if(name != null){
			name = nameConvert.convertName(name, snap.getPrice());
			Item item = getTaobaoItem(new Long(snap.getDistributeCommodity()));
			if(item != null){
				item.setTitle(name);
				updateTaobaoItem(item);
				snap.setName(name);
				snap.setLastupdatetime(new Date());
				updateDistributeCommodity(snap);	
			}
		}
	}

	private Item getTaobaoItem(long numIid){
		ItemGetRequest request = new ItemGetRequest();
		request.setNumIid(numIid);
		request.setFields("num_iid,title,price");
		ItemGetResponse response;
		try {
			response = client.execute(request);
			if(response.isSuccess()){
				Item item  =  response.getItem();
				System.out.println(item.getTitle());
				return item;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	private void updateTaobaoItem(Item item){
		ItemUpdateRequest request = new ItemUpdateRequest();
		request.setNumIid(item.getNumIid());
		request.setTitle(item.getTitle());
		request.setValidThru(new Long(7));
		ItemUpdateResponse response;
		try {
			response = client.execute(request);
			if(response.isSuccess()){

			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	private void updateDistributeCommodity(CommoditySnap snap){
		snapService.updateCommoditySnap(snap);
	}

	public static void main(String[] args) {
		try {
			new UpdateTaobaoItem().getCommodity();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

}
